import 'package:common_utils/common_utils.dart';
import 'package:flutter/material.dart';
import 'package:flutter_getx_project/pages/login/login_controller.dart';
import 'package:flutter_getx_project/pages/login/login_state.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:x_core/x_core.dart';
import 'package:x_project/x_project.dart';

/// Created by XieXin on 2020/3/18.
/// 登录
class LoginPage extends GetView<LoginController> {
  final LoginState state = Get.find<LoginController>().state;
  final GlobalController globalController = Get.find<GlobalController>();

  LoginPage({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return WillPopScope(
      child: Scaffold(
        appBar: XAppBar(
          title: "登录账号",
          onBack: () {
            if (state.isBackHome) {
              globalController.event.value =
                  EventEntity(EventEntity.mainTabIndex0);
              Get.back();
            }
          },
        ),
        body: GetBuilder<LoginController>(
          builder: (c) => XScrollView(
            keyboardConfig: Utils.getKeyboardActionsConfig(
                context, [controller.nodeText1, controller.nodeText2]),
            padding: EdgeInsets.only(top: 20.h, left: 55.w, right: 55.w),
            children: _buildBody,
          ),
        ),
      ),
      onWillPop: () {
        if (state.isBackHome) {
          globalController.event.value = EventEntity(EventEntity.mainTabIndex0);
          Get.until(ModalRoute.withName(CommonRouter.main));
        }
        return Future.value(!state.isBackHome);
      },
    );
  }

  get _buildBody => [
        Text("登录接收数据：${state.login.toString()}"),
        SizedBox(height: 150.h),
        XTextField(
          key: const Key("mobile"),
          controller: controller.mobileController,
          focusNode: controller.nodeText1,
          fontSize: 32.sp,
          maxLength: 11,
          bottomPadding: 10.h,
          keyboardType: TextInputType.phone,
          hintText: "请输入手机号",
          getVCode: () {
            return Future.value(true);
          },
        ),
        SizedBox(height: 32.h),
        XTextField(
          key: const Key("password"),
          controller: controller.passwordController,
          focusNode: controller.nodeText2,
          fontSize: 32.sp,
          maxLength: 16,
          bottomPadding: 10.w,
          hintText: "请输入密码",
          isInputPwd: true,
          rightText: "忘记密码",
          // onRightPressed: () => NavigatorUtils.push(
          //     context, AccountRouter.VERIFY_FORGET_PASSWORD),
        ),
        SizedBox(height: 290.h),
        XButton(
          key: const Key("login"),
          text: "登录",
          onPressed: state.isClickLogin ? _login : null,
        ),
        SizedBox(height: 40.h),
        XButton(
          key: const Key("register"),
          text: "注册",
          onPressed: () {
            LogUtil.v("=======> 注册");
          },
          textColor: Colors.white,
        ),
        SizedBox(height: 40.h),
        XButton(
          text: "发送全局控制器Code：123456",
          onPressed: () {
            globalController.event.value = EventEntity(123456);
            Get.back();
          },
          textColor: Colors.white,
        ),
        SizedBox(height: 40.h),
        XButton(
          text: "跳转到我的",
          onPressed: () {
            globalController.event.value =
                EventEntity(EventEntity.mainTabIndex1);
            Get.until(ModalRoute.withName(CommonRouter.main));
          },
          textColor: Colors.white,
        ),
        SizedBox(height: 40.h),
      ];

  void _login() {
    controller.login(mobile: "15812341234", password: "123456");
  }
}
